package com.taptech.doufu.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.constant.IntentKey;
import com.taptech.doufu.constant.SharedPreferencesKey;
import com.taptech.doufu.event.EventBusReadIndent;
import com.taptech.doufu.event.EventBusReadUnLock;
import com.taptech.doufu.event.EventBusReadVerticalSpace;
import com.taptech.doufu.event.EventBusReadVolumeKey;
import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity;
import com.taptech.doufu.ui.view.theme.read.ReadThemeHelper;
import com.taptech.doufu.ui.view.theme.ThemeType;
import com.taptech.doufu.util.sp.DiaoBaoSharedPreferences;
import com.taptech.doufu.ui.view.colorful.Colorful;

import org.greenrobot.eventbus.EventBus;


/**
 * Created by Wyatt on 2017/11/26/026.
 */

public class ReadMoreSetActivity extends DiaobaoBaseActivity {

    private LinearLayout llFormat, llAlign;
    private ImageView ivSpacingOne, ivSpacingTwo, ivSpacingThree, ivVolumeKey, ivUnLock;
    private TextView tvFormat, tvAlign;
    private boolean isTurnVolumenKey, isUnLock;
    private int indentType, oldIndentType;
    private float verticalSpace;
    private Colorful colorful;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setColorful();
        setContentView(R.layout.activity_read_more_set);
        isTurnVolumenKey = DiaoBaoSharedPreferences.getSharedPreferencesValueToBoolean(SharedPreferencesKey.SP_READ_PAGE_READ_VOLUME_KEY,
                this, false);
        isUnLock = DiaoBaoSharedPreferences.getSharedPreferencesValueToBoolean(SharedPreferencesKey.SP_READ_PAGE_READ_UNLOCK_SCREEN,
                this, false);
        verticalSpace = DiaoBaoSharedPreferences.getSharedPreferencesValueToFloat(SharedPreferencesKey.SP_READ_PAGE_READ_VERTICAL_SPACE,
                this, Constant.ReadSpace.NORMAL);
        indentType = DiaoBaoSharedPreferences.getSharedPreferencesValueToInt(SharedPreferencesKey.SP_READ_PAGE_READ_INDENT,
                this, Constant.ReadIndentType.FORMAT);
        oldIndentType = indentType;
        initUI();
    }


    private void setColorful() {
        colorful = new Colorful.Builder(this).create();
        if (ReadThemeHelper.getReadTheme().getThemeType() == ThemeType.TYPE_NIGHT) {
            colorful.setTheme(R.style.theme_dark);
        } else {
            colorful.setTheme(R.style.theme_day);
        }
    }


    private void initUI() {
        llFormat = (LinearLayout) findViewById(R.id.llFormat);
        llAlign = (LinearLayout) findViewById(R.id.llAlign);
        ivSpacingOne = (ImageView) findViewById(R.id.ivSpacingOne);
        ivSpacingTwo = (ImageView) findViewById(R.id.ivSpacingTwo);
        ivSpacingThree = (ImageView) findViewById(R.id.ivSpacingThree);
        tvFormat = (TextView) findViewById(R.id.tvFormat);
        tvAlign = (TextView) findViewById(R.id.tvAlign);
        ivVolumeKey = (ImageView) findViewById(R.id.ivVolumeKey);
        ivUnLock = (ImageView) findViewById(R.id.ivUnLock);
        updateUnLock();
        updateVolumeKey();
        updateIndentUI();
        updateSpaceUI();
        setOnclick();
    }

    private void updateUnLock() {
        if (ReadThemeHelper.getReadTheme().getThemeType() == ThemeType.TYPE_NIGHT) {
            //夜间
            if (isUnLock) {
                ivUnLock.setImageResource(R.drawable.read_switch_o_night);
            } else {
                ivUnLock.setImageResource(R.drawable.read_switch_c_night);
            }
        } else {
            if (isUnLock) {
                ivUnLock.setImageResource(R.drawable.read_switch_o);
            } else {
                ivUnLock.setImageResource(R.drawable.read_switch_c);
            }
        }
    }

    private void updateVolumeKey() {
        if (ReadThemeHelper.getReadTheme().getThemeType() == ThemeType.TYPE_NIGHT) {
            //夜间
            if (isTurnVolumenKey) {
                ivVolumeKey.setImageResource(R.drawable.read_switch_o_night);
            } else {
                ivVolumeKey.setImageResource(R.drawable.read_switch_c_night);
            }
        } else {
            if (isTurnVolumenKey) {
                ivVolumeKey.setImageResource(R.drawable.read_switch_o);
            } else {
                ivVolumeKey.setImageResource(R.drawable.read_switch_c);
            }
        }
    }

    private void updateIndentUI() {
        if (ReadThemeHelper.getReadTheme().getThemeType() == ThemeType.TYPE_NIGHT) {
            //夜间
            if (indentType == Constant.ReadIndentType.FORMAT) {
                llFormat.setBackgroundResource(R.drawable.red_stroke_night);
                llAlign.setBackgroundResource(R.drawable.gray_stroke_night);
                tvAlign.setTextColor(getResources().getColor(R.color.text_dark));
                tvFormat.setTextColor(getResources().getColor(R.color.theme_main_dark));
            } else {
                llFormat.setBackgroundResource(R.drawable.gray_stroke_night);
                llAlign.setBackgroundResource(R.drawable.red_stroke_night);
                tvFormat.setTextColor(getResources().getColor(R.color.text_dark));
                tvAlign.setTextColor(getResources().getColor(R.color.theme_main_dark));
            }
        } else {
            if (indentType == Constant.ReadIndentType.FORMAT) {
                llFormat.setBackgroundResource(R.drawable.red_stroke);
                llAlign.setBackgroundResource(R.drawable.gray_stroke);
                tvAlign.setTextColor(getResources().getColor(R.color.text_color_1));
                tvFormat.setTextColor(getResources().getColor(R.color.text_color_67));
            } else {
                llFormat.setBackgroundResource(R.drawable.gray_stroke);
                llAlign.setBackgroundResource(R.drawable.red_stroke);
                tvFormat.setTextColor(getResources().getColor(R.color.text_color_1));
                tvAlign.setTextColor(getResources().getColor(R.color.text_color_67));
            }
        }
    }

    private void updateSpaceUI() {
        if (ReadThemeHelper.getReadTheme().getThemeType() == ThemeType.TYPE_NIGHT) {
            if (verticalSpace == Constant.ReadSpace.TIGHT) {
                ivSpacingOne.setImageResource(R.drawable.read_spacing_3_st_night);
                ivSpacingTwo.setImageResource(R.drawable.read_spacing_2_night);
                ivSpacingThree.setImageResource(R.drawable.read_spacing_1_night);
            } else if (verticalSpace == Constant.ReadSpace.NORMAL) {
                ivSpacingOne.setImageResource(R.drawable.read_spacing_3_night);
                ivSpacingTwo.setImageResource(R.drawable.read_spacing_2_st_night);
                ivSpacingThree.setImageResource(R.drawable.read_spacing_1_night);
            } else if (verticalSpace == Constant.ReadSpace.LOOSE) {
                ivSpacingOne.setImageResource(R.drawable.read_spacing_3_night);
                ivSpacingTwo.setImageResource(R.drawable.read_spacing_2_night);
                ivSpacingThree.setImageResource(R.drawable.read_spacing_1_st_night);
            }
        } else {
            if (verticalSpace == Constant.ReadSpace.TIGHT) {
                ivSpacingOne.setImageResource(R.drawable.read_spacing_3_st);
                ivSpacingTwo.setImageResource(R.drawable.read_spacing_2);
                ivSpacingThree.setImageResource(R.drawable.read_spacing_1);
            } else if (verticalSpace == Constant.ReadSpace.NORMAL) {
                ivSpacingOne.setImageResource(R.drawable.read_spacing_3);
                ivSpacingTwo.setImageResource(R.drawable.read_spacing_2_st);
                ivSpacingThree.setImageResource(R.drawable.read_spacing_1);
            } else if (verticalSpace == Constant.ReadSpace.LOOSE) {
                ivSpacingOne.setImageResource(R.drawable.read_spacing_3);
                ivSpacingTwo.setImageResource(R.drawable.read_spacing_2);
                ivSpacingThree.setImageResource(R.drawable.read_spacing_1_st);
            }
        }
    }

    private void changeReadSpace(float space) {
        if (verticalSpace == space) {
            return;
        }
        verticalSpace = space;
        updateSpaceUI();
        DiaoBaoSharedPreferences.setSharedPreferencesValueToFloat(SharedPreferencesKey.SP_READ_PAGE_READ_VERTICAL_SPACE,
                space, this);
        EventBusReadVerticalSpace event = new EventBusReadVerticalSpace();
        event.setTag(EventBusReadVerticalSpace.tag);
        event.setSpace(verticalSpace);
        EventBus.getDefault().post(event);
    }

    private void changeReadIndent(int type) {
        if (indentType == type) {
            return;
        }
        indentType = type;
        updateIndentUI();
        DiaoBaoSharedPreferences.setSharedPreferencesValueToInt(SharedPreferencesKey.SP_READ_PAGE_READ_INDENT,
                type, this);
        EventBusReadIndent event = new EventBusReadIndent();
        event.setTag(EventBusReadIndent.tag);
        event.setType(indentType);
        EventBus.getDefault().post(event);
    }

    private void setOnclick() {
        llFormat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeReadIndent(Constant.ReadIndentType.FORMAT);
            }
        });
        llAlign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeReadIndent(Constant.ReadIndentType.ALIGN);
            }
        });
        ivSpacingOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeReadSpace(Constant.ReadSpace.TIGHT);
            }
        });
        ivSpacingTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeReadSpace(Constant.ReadSpace.NORMAL);
            }
        });
        ivSpacingThree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeReadSpace(Constant.ReadSpace.LOOSE);
            }
        });
        ivUnLock.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isUnLock = !isUnLock;
                updateUnLock();
                EventBusReadUnLock event = new EventBusReadUnLock();
                event.setTag(EventBusReadUnLock.tag);
                event.setUnLock(isUnLock);
                EventBus.getDefault().post(event);
                DiaoBaoSharedPreferences.setSharedPreferencesValueToBoolean(SharedPreferencesKey.SP_READ_PAGE_READ_UNLOCK_SCREEN,
                        isUnLock, ReadMoreSetActivity.this);
            }
        });
        ivVolumeKey.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isTurnVolumenKey = !isTurnVolumenKey;
                EventBusReadVolumeKey event = new EventBusReadVolumeKey();
                event.setTag(EventBusReadVolumeKey.tag);
                event.setTurnVolumeKey(isTurnVolumenKey);
                EventBus.getDefault().post(event);
                DiaoBaoSharedPreferences.setSharedPreferencesValueToBoolean(SharedPreferencesKey.SP_READ_PAGE_READ_VOLUME_KEY,
                        isTurnVolumenKey, ReadMoreSetActivity.this);
                updateVolumeKey();
            }
        });
    }

    @Override
    public void finish() {
        if (oldIndentType != indentType) {
            Intent intent = new Intent();
            intent.putExtra(IntentKey.INTENT_KEY_READ_INDENT, indentType);
            setResult(RESULT_OK, intent);
        }
        super.finish();
    }


}
